/** Cpoyright (c) 2020 by XMX
 *	file : main.c
 *	since : 2020-08-23
 */

#include"tyger.h"

int main(int argc, char **argv)
{
	int listenfd, connfd;
	char hostname[MAXLINE], port[MAXLINE];
	socklen_t clientlen;
	struct sockaddr_storage clientaddr;

	/* Check command line args */
	if (argc != 2) {
		fprintf(stderr, "usage: %s <port>\n", argv[0]);
		exit(1);
	}

	/* set a signal listenor */
	Signal(SIGCHLD, SigHandler);
	listenfd = Open_listenfd(argv[1]);
	while (1) {
		clientlen = sizeof(clientaddr);
		connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
		Getnameinfo((SA *)&clientaddr, clientlen, 
					hostname, MAXLINE, port, MAXLINE, 0);
		printf("Accept connection from (%s, %s)\n", hostname, port);
		/* fork to process request */
		doit(connfd);	/* HTTP implementation */
		close(connfd);
	}
}
